# import zipfile
# from itertools import product
 
# def brute_force(zip_path):
#     # 获取所有可能的密码组合（字符集为数字、小写字母）
#     characters = '0123456789abcdefghijklmnopqrstuvwxyz'
#     password_lengths = [i for i in range(1, 10)]  # 设置最大密码长度为9位
#     print(zip_path)
#     with open('password.txt', 'a') as f:
#         for length in password_lengths:
#             passwords = [''.join(p) for p in product(characters, repeat=length)]
            
#             try:
#                 zf = zipfile.ZipFile(zip_path)
                
#                 for password in passwords:
#                     print(passwords)
#                     print(password)
#                     try:
#                         zf.extractall(pwd=bytes(password, encoding='utf-8'))
                        
#                         print("成功解压缩文件！")
#                         break
#                     except Exception as e:
#                         pass
                
#                 if not zf.namelist():
#                     print("未找到任何文件或无法解压缩该压缩包。")
                
#                 zf.close()
#                 return
#             except Exception as e:
#                 continue
import zipfile
from itertools import product
 
def crack_zip(filename):
    # 定义可能的密码字符集合
    characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    
    with open('passwords.txt', 'a') as f:
        for length in range(5, 10):  # 设置最大长度为9位数
            for password in product(characters, repeat=length):
                # print(password)
                try:
                    zf = zipfile.ZipFile(filename)
                    zf.extractall(pwd=''.join(password).encode())
                    print("Password found!")
                    return True
                except Exception as e:
                    pass
        
        print("No password found.")
        return False
 
# 调用函数并传入ZIP文件名
crack_zip('/Users/lw/Downloads/Java面试八股文：高频面试题与求职攻略一本通/电子书（高清无删减版本）/电子书（高清无删减版本）.zip')